﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.PBCM;
using Entities;
using Entities.ThuQuy;

namespace WindowsForms.ThuQuy
{
    public partial class FormThemChiTietHoaDon : Form
    {
        public static string mahoadon;
        string mathietbi;
        float dongia;
        public FormThemChiTietHoaDon()
        {
            InitializeComponent();
            dataGridViewthietbi.AutoGenerateColumns = false;
        }

        private void FormThemChiTietHoaDon_Load(object sender, EventArgs e)
        {
            lb1.Text = "Mã Hóa Đơn: " + mahoadon;
            dataGridViewthietbi.DataSource = ThietBi.Objects.All().ToList();
            LoadThietBi();
        }
        public void LoadThietBi()
        {
            List<ThietBi> Ds = ThietBi.Objects.All().ToList();
            foreach (var item in Ds)
                cbthietbi.Items.Add(item);
            cbthietbi.DisplayMember = "TenThietBi";
            cbthietbi.ValueMember = "MaThietBi";
            cbthietbi.Items.Insert(0, "-- Thiết Bị --");
            cbthietbi.SelectedIndex = 0;


        }

      
        private ChiTietHoaDon Laycontrol()
        {
            ChiTietHoaDon ct = new ChiTietHoaDon();
            HoaDon hd = HoaDon.Objects.GetById(mahoadon);
            if (hd != null)
                ct.LoaiChi = hd.MaLoai;
            ct.MaHD = mahoadon;
            ct.NoiDung = tbnoidung.Text;
            ct.VatPham = mathietbi;

            ct.SoLuong = Convert.ToInt32(tbsoluog.Text);
            ct.ThanhTien = ct.SoLuong * dongia;
            return ct;


        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            ChiTietHoaDon ct = Laycontrol();

            if (ChiTietHoaDon.Objects.AddWithIdentity(ct) > 0)
                MessageBox.Show("Thành Công");
            else MessageBox.Show("Thất Bại");
        }
        private bool KiemTra(int s, List<ChiTietHoaDon> ds)
        {
            var q = from k in ds where k.MaCTHD == s select k;
            return q != null;
        }

        private void tbsoluog_TextChanged(object sender, EventArgs e)
        {
            try
            {
                tbtien.Text =
            String.Format("{0:0,0 VNĐ}", Convert.ToInt64(tbsoluog.Text) * dongia);
            }
            catch (Exception)
            {

                tbtien.Text =
            String.Format("{0:0,0 VNĐ}", 0);
            }
            
        }

        private void tbsoluog_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar))

                e.Handled = true;
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       
        private void cbthietbi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbthietbi.SelectedIndex > 0)
            {
                ThietBi d = cbthietbi.SelectedItem as ThietBi;
                mathietbi = d.MaThietBi;
                dongia = d.DonGia;
                try
                {
                    tbtien.Text =
     String.Format("{0:0,0 VNĐ}", Convert.ToInt64(tbsoluog.Text) * dongia);
                }
                catch (Exception)
                {

                    tbtien.Text =
String.Format("{0:0,0 VNĐ}", 0);
                }
          

            }

        }


    }
}
